package com.grape.lock.zookeeper;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 描述:OrderService
 * @author: myx
 * @date: 2019/8/23 0023
 * Copyright © 2019-grape. All rights reserved.
 */
public class OrderService implements Runnable {
	//全局订单id
	public static int count = 0;

	private Lock lock = new ZookeeperDistrbuteLock();

	@Override
	public void run() {
		getNumber();
	}

	/**
	 * 获取订单号
	 */
	public void getNumber() {
		try {
			lock.getLock();
			try {
				Thread.sleep(200);
			} catch (Exception e) {
			}
			SimpleDateFormat simpt = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
			System.out.println(Thread.currentThread().getName() + ",生成订单ID:" +simpt.format(new Date()) + "-" + ++count);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			lock.unLock();
		}
	}
	public static void main(String[] args) {
		System.out.println("####生成唯一订单号###");
		for (int i = 0; i < 100; i++) {
			new Thread( new OrderService()).start();
		}
	}
}